/*
 * Copyright 2015 Rudson Lima
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package br.liveo.ndrawer.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.Menu;
import android.view.View;
import android.view.Window;
import android.widget.Toast;
import br.liveo.Model.HelpLiveo;
import br.liveo.interfaces.OnItemClickListener;
import br.liveo.interfaces.OnPrepareOptionsMenuLiveo;
import br.liveo.navigationliveo.NavigationLiveo;
import br.liveo.ndrawer.R;
import br.liveo.ndrawer.listener.ScrollListener;
import br.liveo.ndrawer.ui.fragment.DisplayFragment;
import br.liveo.ndrawer.ui.fragment.MainFragment;
import egame.libs.android.util.DebugUtils;

public class MainActivity extends NavigationLiveo implements
		OnItemClickListener, ScrollListener {

	private HelpLiveo mHelpLiveo;

	private Fragment mDisplayFragment;

	@Override
	public void onInt(Bundle savedInstanceState) {

		// User Information
		this.userName.setText("Rudson Lima");
		this.userEmail.setText("rudsonlive@gmail.com");
		this.userPhoto.setImageResource(R.drawable.ic_rudsonlive);
		this.userBackground
				.setImageResource(R.drawable.ic_user_background_second);

		// setUserBackground();

		// Creating items navigation
		mHelpLiveo = new HelpLiveo();
		mHelpLiveo.add(getString(R.string.inbox), R.mipmap.ic_inbox_black_24dp,
				7);
		mHelpLiveo.addSubHeader(getString(R.string.categories)); // Item
																	// subHeader
		mHelpLiveo
				.add(getString(R.string.starred), R.mipmap.ic_star_black_24dp);
		mHelpLiveo.add(getString(R.string.sent_mail),
				R.mipmap.ic_send_black_24dp);
		mHelpLiveo.add(getString(R.string.drafts),
				R.mipmap.ic_drafts_black_24dp);
		mHelpLiveo.addSeparator(); // Item separator
		mHelpLiveo
				.add(getString(R.string.trash), R.mipmap.ic_delete_black_24dp);
		mHelpLiveo.add(getString(R.string.spam), R.mipmap.ic_report_black_24dp,
				120);

		// {optional} - Header Customization - method customHeader
		// View mCustomHeader =
		// getLayoutInflater().inflate(R.layout.custom_header_user,
		// this.getListView(), false);
		// ImageView imageView = (ImageView)
		// mCustomHeader.findViewById(R.id.imageView);

		with(this)
				.startingPosition(2)
				// Starting position in the list
				.addAllHelpItem(mHelpLiveo.getHelp())

				// {optional} - List Customization
				// "If you remove these methods and the list will take his white standard color"
				// .selectorCheck(R.drawable.selector_check) //Inform the
				// background of the selected item color
				// .colorItemDefault(R.color.nliveo_blue_colorPrimary) //Inform
				// the standard color name, icon and counter
				// .colorItemSelected(R.color.nliveo_purple_colorPrimary)
				// //State the name of the color, icon and meter when it is
				// selected
				// .backgroundList(R.color.nliveo_black_light) //Inform the list
				// of background color
				// .colorLineSeparator(R.color.nliveo_transparent) //Inform the
				// color of the subheader line

				// {optional} - SubHeader Customization
				.colorItemSelected(R.color.nliveo_blue_colorPrimary)
				.colorNameSubHeader(R.color.nliveo_blue_colorPrimary)
				// .colorLineSeparator(R.color.nliveo_blue_colorPrimary)

				.footerItem(R.string.settings, R.mipmap.ic_settings_black_24dp)

				// {optional} - Header Customization
				// .customHeader(mCustomHeader)

				// {optional} - Footer Customization
				// .footerNameColor(R.color.nliveo_blue_colorPrimary)
				// .footerIconColor(R.color.nliveo_blue_colorPrimary)
				// .footerBackground(R.color.nliveo_white)

				.setOnClickUser(onClickPhoto)
				.setOnPrepareOptionsMenu(onPrepare)
				.setOnClickFooter(onClickFooter).build();

		int position = this.getCurrentPosition();
		this.setElevationToolBar(position != 2 ? 15 : 0);

		setTitle("Display");

		// translateToolbar(50);
	}

	@Override
	public void onItemClick(final int position) {

		new Handler().postDelayed(new Runnable() {

			@Override
			public void run() {
				// TODO Auto-generated method stub
				Fragment mFragment;
				FragmentManager mFragmentManager = getSupportFragmentManager();

				switch (position) {
				case 2:

					if (mDisplayFragment == null)
						mDisplayFragment = DisplayFragment.getInstance(
								MainActivity.this, MainActivity.this);

					mFragment = mDisplayFragment;
					break;

				default:
					mFragment = MainFragment.newInstance(mHelpLiveo.get(
							position).getName());

					break;
				}

				if (mFragment != null) {

					FragmentTransaction ft = mFragmentManager
							.beginTransaction();
					ft.setCustomAnimations(android.R.anim.slide_in_left,
							android.R.anim.slide_out_right,
							android.R.anim.fade_in, android.R.anim.fade_out);

					ft.replace(R.id.container, mFragment).commit();

					setTitle(mHelpLiveo.get(position).getName());

				}

			}
		}, 300);

		setElevationToolBar(position != 2 ? 15 : 0);
	}

	private OnPrepareOptionsMenuLiveo onPrepare = new OnPrepareOptionsMenuLiveo() {
		@Override
		public void onPrepareOptionsMenu(Menu menu, int position,
				boolean visible) {
		}
	};

	private View.OnClickListener onClickPhoto = new View.OnClickListener() {
		@Override
		public void onClick(View v) {
			Toast.makeText(getApplicationContext(), "onClickPhoto :D",
					Toast.LENGTH_SHORT).show();
			closeDrawer();
		}
	};

	private View.OnClickListener onClickFooter = new View.OnClickListener() {
		@Override
		public void onClick(View v) {
			startActivity(new Intent(getApplicationContext(),
					SettingsActivity.class));
			closeDrawer();
		}
	};

	@Override
	public void onTranslate(int y) {

//		if (y < 150) {
//			if (!isShow) {
//				isShow = true;
//				getSupportActionBar().show();
//			}
//		}
		// TODO Auto-generated method stub
		// DebugUtils.d("MainActivity", "onTranslate:   Kai => " + y);
		// translateToolbar(-y);
		// ((DisplayFragment) mDisplayFragment).translateTab(-y);
	}

	private boolean isShow = true;

	@Override
	public void onShow() {
		// TODO Auto-generated method stub
		DebugUtils.d("MainActivity", "onHide:   Kai =>show ");
		if (!isShow) {
			isShow = true;
			getSupportActionBar().show();
		}
	}

	@Override
	public void onHide() {
		// TODO Auto-generated method stub

		DebugUtils.d("MainActivity", "onHide:   Kai =>hide ");
		if (isShow) {
			isShow = false;
			getSupportActionBar().hide();
		}
	}
}
